﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using CodeTier.BUL;

namespace Quanly
{
    public partial class Frm_QuanLyDanhMuc : Frm_Abstract
    {
        List<DanhMucBUS> lst_dm;

        public Frm_QuanLyDanhMuc()
        {
            InitializeComponent();
            refresh();
        }

        public void loadData()
        {
            lst_dm = DanhMucBUS.getAll();
        }

        public void show()
        {
            loadSanPham();
        }

        public void loadSanPham()
        {
            if (lst_dm == null)
            {
                MessageBox.Show("Error load database", "Error Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                this.lstview_danhmuc.Items.Clear();
                foreach (DanhMucBUS dm in lst_dm)
                {
                    addProductIntoListview(dm);
                }
            }
        }

        public void addProductIntoListview(DanhMucBUS dm)
        {
            ListViewItem item;
            item = this.lstview_danhmuc.Items.Add(dm.MaDM.ToString());
            item.SubItems.Add(dm.TenDM);
            item.SubItems.Add(dm.MoTa);
            item.SubItems.Add(dm.NgayTao.ToString());
        }

        private void lstview_danhmuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            txt_TenDanhMuc.Text = lstview_danhmuc.SelectedItems[0].SubItems[1].Text;
            txt_MoTa.Text = lstview_danhmuc.SelectedItems[0].SubItems[2].Text;
        }

        public override void save()
        {
            DanhMucBUS dm = new DanhMucBUS();
            dm.TenDM = txt_TenDanhMuc.Text;
            dm.MoTa = txt_MoTa.Text;
            dm.NgayTao = DateTime.Today;
            dm.TinhTrang = "1";
            dm.insert();
            refresh();
        }

        public override void update()
        {
            if (lstview_danhmuc.SelectedItems.Count > 0)
            {
                DanhMucBUS dm = new DanhMucBUS();
                dm.MaDM = Convert.ToInt32(lstview_danhmuc.SelectedItems[0].Text);
                dm.TenDM = txt_TenDanhMuc.Text;
                dm.MoTa = txt_MoTa.Text;
                dm.update();
                refresh();
            }
        }

        public override void closeForm()
        {
            this.Close();
        }

        public override void delete()
        {
            if (lstview_danhmuc.SelectedItems.Count > 0)
            {
                DanhMucBUS dm = new DanhMucBUS();
                dm.MaDM = Convert.ToInt32(lstview_danhmuc.SelectedItems[0].Text);
                dm.delete();
                refresh();
            }
        }

        public override void  refresh()
        {
            loadData();
            show();
            clear();
        }

        public void clear()
        {
            txt_MoTa.Text = null;
            txt_TenDanhMuc.Text = null;
        }

    }
}